<?php

/**
 * @author     bigface.cc
 * @date       2012-7-8 
 * @version    1.0
 */
//查茶网链接控制器
class linksAction extends baseAction{
        //显示链接列表
        function index(){
                $links_mod=M('links');
                import("ORG.Util.Page");
                $prex=C("DB_PREFIX");
                //搜索显示列表
                $where='1=1';
                if(isset ($_GET['typeid']) && $_GET['typeid']){
                        $typeid=trim($_GET['typeid']);
                        $where.=" AND typeid=".$typeid;
                        $this->assign('typeid',$typeid);
                }
                if(isset ($_GET['cityid']) && $_GET['cityid']){
                        $cityid=trim($_GET['cityid']);
                        $where.=" AND cityid=".$cityid;
                        $this->assign('cityid',$cityid);
                }
                if(isset ($_GET['keyword']) && $_GET['keyword']){
                        $keyword=trim($_GET['keyword']);
                        $where.=" AND (".$prex."links.name LIKES %".$keyword."%";
                        $this->assign("keyword",$keyword);
                }
                $count=$links_mod->where($where)->count();
//                dump($count);exit;
                $p=new Page($count,10);
                $links_list=$links_mod->field($prex.'links.id,'.$prex.'links.logo,'.$prex.'links.sort,'.$prex.'links.status,'.$prex.'links.name,'.$prex.'links.url,'.$prex.'links_type.type_name,'.$prex.'links_city.name as city')->join('LEFT JOIN '.$prex.'links_type ON '. $prex .'links.typeid='. $prex .'links_type.id')->join('LEFT JOIN '.$prex.'links_city ON '. $prex .'links.cityid='. $prex .'links_city.id')->where($where)->limit($p->firstRow . ',' . $p->listRows)->order($prex . 'links.sort DESC')->select();
                $key = 1;
		foreach ($links_list as $k => $val) {
			$links_list[$k]['key'] = ++$p->firstRow;
		}
                $this->assign('links_list', $links_list);
                //显示分页
                $page=$p->show();
                $this->assign('page',$page);
                //显示分类
                $type=M('links_type');
                $type_list=$type->field('id,type_name')->where('hide != 0')->order('sort DESC')->select();
                $this->assign("type_list",$type_list);
                //显示所有城市
                $city=M('links_city');
                $city_list=$city->field('id,name')->order('id DESC')->select();
                $this->assign("city_list",$city_list);
//                dump($city_list);exit();
                $this->display();
        }
        //添加链接
        function add(){
                if(isset($_POST['dosubmit'])){
                        //提交表单
                        $links_mod=D('links');
                        if(false === $data=$links_mod->create()){
                                $this->error($links_mod->getError());
                        }
                        if($_FILES['logo']['name']!=''){
                                $uploadList=$this->_upload($_FILES['logo']);
                                $data['logo']=$uploadList[0]['savename'];
                        }
                        $data['name']=isset ($_POST['name']) && $_POST['name']?trim($_POST['name']):  $this->error("请输入连接名称");
                        $data['url']=isset ($_POST['url']) && $_POST['url']?trim($_POST['url']):  $this->error("请输入链接地址");
                        $rs=$links_mod->add($data);
                        if($rs){
                                $this->success("添加链接成功",U('links/index'),1);
                        }
                }
                //显示分类
                $type=M('links_type');
                $type_list=$type->field('id,type_name')->where('hide != 0')->order('sort DESC')->select();
                $this->assign("type_list",$type_list);
                //显示所有城市
                $city=M('links_city');
                $city_list=$city->field('id,name')->order('id DESC')->select();
                $this->assign("city_list",$city_list);
                $this->display();
        }
        //删除连接
        function delete() {
                parent::delete();
        }
        //排序，数字越大越靠前显示
        function sort_order() {
                parent::sort_order();
        }
        //审核
        function status() {
                parent::status();
        }
        //编辑
        function edit(){
                $links_mod=M('links');
                if(isset ($_POST['dosubmit'])){
                        if(false ===$data=$links_mod->create()){
                                $this->error($links_mod->getError());
                        }
                        if($_FILES['logo']['name']!=''){
                                $uploadList=$this->_upload($_FILES['logo']);
                                $data['logo']=$uploadList[0]['savename'];
                        }
                        $result=$links_mod->where('id='.$data['id'])->save($data);
                        if(false!==$result){
                                $this->success(L('operation_success'), '', '', 'edit');
                        }else{
                                $this->error(L('operation_failure'));
                        }
                }else{
                        if (isset ($_GET['id'])) {
                                $links_id=isset ($_GET['id'])?intval($_GET['id']):$this->error("请选择您要编辑的ID");
                        }
                        $links_info=$links_mod->where("id=".$links_id)->find();
//                        dump($links_info);exit;
                        $this->assign("links_info",$links_info);
                        //显示分类
                        $type=M('links_type');
                        $type_list=$type->field('id,type_name')->where('hide != 0')->order('sort DESC')->select();
                        $this->assign("type_list",$type_list);
                        //显示所有城市
                        $city=M('links_city');
                        $city_list=$city->field('id,name')->order('id DESC')->select();
                        $this->assign("city_list",$city_list);
                        $this->display();
                }
        }

        //上传文件
        protected function _upload() {
        import("ORG.Net.UploadFile");
        //导入上传类
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize = 3292200;
        //设置上传文件类型
        $upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
        //设置附件上传目录
        $upload->savePath = '../includes/data/links/';
        //设置上传文件规则
        $upload->saveRule = uniqid;
        //删除原图
        if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
        }
        return $uploadList;
    }
}
?>
